﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;

namespace mamnontuoitho7.backend
{
    public partial class ClassTeacherAdd : System.Web.UI.Page
    {

        protected Teacher _teacher = new Teacher();
        protected General _general = new General();

        protected DataTable _dttTeacher;
        protected String _className = "";
        protected String _classID = "";

        protected void Page_Load(object sender, EventArgs e)
        {

            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionInfo).IndexOf(Session["role"].ToString() + ",") != -1)
            {
                try
                {
                    if (Request.QueryString["hidClassID"] != null && Request.QueryString["hidClassName"] != null)
                    {
                        _classID = Request.QueryString["hidClassID"].ToString();
                        _className = Request.QueryString["hidClassName"].ToString();

                        _dttTeacher = _teacher.GetAvailableTeacherList(_classID);

                        //Get ID of the chosen teachers and put into sequence string separate by comma
                        string chosenKidList = "";
                        for (int i = 0; i < _dttTeacher.Rows.Count; i++)
                        {
                            string abc = _dttTeacher.Rows[i]["MemberID"].ToString();

                            if (Request.Form["Teacher_" + _dttTeacher.Rows[i]["MemberID"].ToString()] != null && Request.Form["Teacher_" + _dttTeacher.Rows[i]["MemberID"].ToString()].ToString().ToLower().Equals("on"))
                            {
                                chosenKidList += _dttTeacher.Rows[i]["MemberID"].ToString() + ",";
                            }
                        }

                        //Save the chosen teachers to a class
                        if (chosenKidList != "")
                        {
                            chosenKidList = chosenKidList.Substring(0, chosenKidList.Length - 1);
                            _teacher.KidChangeClass(_classID, chosenKidList);
                            _dttTeacher = _teacher.GetAvailableTeacherList(_classID);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Response.Redirect(ConstantClass._errorAdminPage);
                }
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }
    }
}